#pragma once

#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

class InetAddr
{
private:
    //GetAddress：以输出型参数的方式获取ip和port,类里面使用
    void GetAddress(std::string *ip, uint16_t *port);

public:
    //构造函数
    InetAddr(const struct sockaddr_in &addr) : _addr(addr)
    {
        char buffer[1024];
        const char* n = inet_ntop(AF_INET,&addr.sin_addr.s_addr,buffer,sizeof(buffer));
        //差错处理
        _port = ntohs(addr.sin_port);
    }
    InetAddr(const std::string &ip, uint16_t port) : _ip(ip), _port(port)
    {
        inet_pton(AF_INET,ip.c_str(),&_addr.sin_addr.s_addr);
        _addr.sin_family = AF_INET;
        _addr.sin_port = htons(port);
    }
    InetAddr(){}
    std::string Ip()//外部获取IP地址
    {
        return _ip;
    }
    bool operator == (const InetAddr &addr)
    {
        if(addr._ip == _ip && addr._port == _port)
        {
            return true;
        }
        return false;
    }
    struct sockaddr_in Addr()//外部获取sockaddr_in
    {
        return _addr;
    }
    uint16_t Port()//外部获取port
    {
        return _port;
    }
    ~InetAddr()
    {}

private:
    struct sockaddr_in _addr;
    std::string _ip;
    uint16_t _port;
};
